#include "Misc.h"
#include <vector>
using namespace std;

#include "cvblob.h"
using namespace cvb;

const int THRESHOLD_H = 18;
const int THRESHOLD_S = 50;
const int THRESHOLD_V = 80;

const int THRESHOLD_CANNY = 100;

/// \fn Misc::DetectSkin( const Mat& inputImage, Mat* outputImage )
/// \brief Detect skin region
/// \param inputImage input frame image
/// \param output binary result image with skin region white
void Misc::DetectSkin( const Mat& inputImage, Mat* outputImage )
{
	//convert image from "rgb" space to "hsv" space
	Mat hsvImage;
	cvtColor(inputImage,hsvImage,CV_BGR2HSV);

	vector<Mat> hsvPlanes;

	//split image into H,S,V components
	split(hsvImage,hsvPlanes);

	//thresholds H,S,V values to pixels in correct range of skin
	threshold(hsvPlanes[0], hsvPlanes[0],THRESHOLD_H, UCHAR_MAX, CV_THRESH_BINARY_INV);
	threshold(hsvPlanes[1], hsvPlanes[1],THRESHOLD_S, UCHAR_MAX, CV_THRESH_BINARY);
	threshold(hsvPlanes[2], hsvPlanes[2],THRESHOLD_V, UCHAR_MAX, CV_THRESH_BINARY);

	//use logical bitwise conjunction to verge H,S,V components
	bitwise_and(hsvPlanes[0],hsvPlanes[1],*outputImage);
	bitwise_and(*outputImage,hsvPlanes[2],*outputImage);
}
